package com.car.dao.impl;

import com.car.dao.UserCarDao;
import com.car.domain.UserCar;
import com.car.utils.JDBCUtil;

import java.util.List;

/**
 * UserCarDao的实现类
 */
public class UserCarDaoImpl implements UserCarDao {
    /**
     * 新增
     *
     * @param userCar 泛型
     */
    @Override
    public int add(UserCar userCar) throws Exception {
        String sql = "insert t_user_car(user_id, car_id) " +
                "VALUE(?,?)";
        return JDBCUtil.update(sql, userCar.getUserId(), userCar.getCarId());
    }

    /**
     * 删除
     *
     * @param id 主键id
     * @return 影响记录数
     */
    @Override
    public int delete(int id) throws Exception {
        String sql = "delete from t_user_car where id = ? ";
        return JDBCUtil.update(sql, id);
    }

    /**
     * 修改
     *
     * @param userCar 泛型
     */
    @Override
    public int update(UserCar userCar) throws Exception {
        String sql = "update t_user_car set user_id = ? " +
                "where id = ?";
        return JDBCUtil.update(sql, userCar.getUserId(), userCar.getCarId(), userCar.getId());
    }

    /**
     * 通过主键id查询
     *
     * @param id 主键id
     * @return 实体类
     */
    @Override
    public UserCar queryById(int id) throws Exception {
        String sql = "select * from t_user_car where id = ?";
        return JDBCUtil.query(sql, UserCar.class, id);
    }

    /**
     * 查询所有记录
     *
     * @return 实体类集合
     */
    @Override
    public List<UserCar> queryAll() throws Exception {
        String sql = "select * from t_user_car";
        return JDBCUtil.queryList(sql, UserCar.class);
    }

}
